package com.gy.hsim.bservice.asyncload.thread;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gy.hsim.bservice.asyncload.ChatRecordFile;
import com.gy.hsim.bservice.asyncload.constants.CharRecordConstant;
import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.redis.JRedisPersistentMessage;
import com.gy.hsim.bservice.friendInfo.service.FriendInfoService;
import com.gy.hsim.bservice.friendInfo.service.impl.FriendInfoServiceImpl;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * 聊天记录
 * 
 * @author Lee
 * 
 */
public class ChatGroupRecordConsumerThread extends Thread {
	// 从rabbitmq取互生消息通道
	private QueueingConsumer consumer;
	// 消息队列
//	private ConcurrentLinkedQueue<Map<String,String>> queue;

//	private ConcurrentLinkedQueue<GYMqEntity> queue;
	
	private static final Logger log = LoggerFactory
			.getLogger(ChatGroupRecordConsumerThread.class);
	
	private SystemConfig config;
	
	private FriendInfoService friendInfoService;
	
	private int i = 0;
	
	private int j = 1;
	
	private String seq_no;
	
	long charRecordFileSize;
	
	private String fileNameTemp = null;
	
	private static final String extension = ".txt";
	
	private String  fileName ;
	
	private int index;
	
	long day = Long.parseLong(new SimpleDateFormat(
			"yyyyMMdd").format(System.currentTimeMillis()));

	
	public ChatGroupRecordConsumerThread( FriendInfoService friendInfoService,QueueingConsumer consumer,
			int index,SystemConfig config) {
		this.friendInfoService = friendInfoService;
		this.consumer = consumer;
//		this.queue = queue;
		this.index = index;
		
		this.config = config;
		
		File fileFolder = new File(CharRecordConstant.CHATRECORD_PATH);
		
		if(!fileFolder.exists()) {
			
			fileFolder.mkdir();
			
		}
		
//		config = (SystemConfig)SpringUtil.getBean("system");
		
		charRecordFileSize = config.getCharRecordFileSize();
	}

	@Override
	public void run() {

		while (!isInterrupted()) {
			
			Delivery delivery = null;
			try {
				delivery = consumer.nextDelivery();
			} catch (ShutdownSignalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (delivery != null && delivery.getBody() != null) {

					String str = null;
					
					try {
						str = new String(delivery.getBody(),"UTF-8");
						System.out.println("接收的消息-------------------" + str);
						
						
						//持久化聊天消息 start David 2015/07/27 暂时不发布
			//			try{
				//		    new JRedisPersistentMessage().saveMessage(str);
					//	}catch(Exception e){
						//	e.printStackTrace();
					//	}
						//持久化聊天消息 end
						
						
						//持久化聊天消息到mysql message表中 start David 2015/07/27
						try{
							
							friendInfoService.insertGroupMessage(str);
							
						}catch(Exception e){
							e.printStackTrace();
						}
						//持久化聊天消息 end
						
						
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					long currentDay = Long.parseLong(new SimpleDateFormat(
							"yyyyMMdd").format(System.currentTimeMillis()));
					
					if(currentDay - day == 1) {
						j = 1;
						day = currentDay;
					}
					
					if( j == 1 ) {
						
						fileNameTemp = new String(CharRecordConstant.CHATRECORD_PATH + File.separator + "chatGroupRecord" + day + "_");
						
						
						fileName = fileNameTemp + index + "_001" + extension ;
						
					} 
					
					if(i < charRecordFileSize) {
						
						new ChatRecordFile().appendFile(fileName, str + "\r\n");
						
						i++;
						
					} else {
						
						j++;
						
						if(j < 10) {
							seq_no = "00" + j;
						} else if(j < 100) {
							seq_no = "0" + j;
						} else {
							seq_no = String.valueOf(j);
						}
						
						fileNameTemp = new String(CharRecordConstant.CHATRECORD_PATH + File.separator + "chatPushRecord" + day + "_");
						
						fileName = fileNameTemp + index + "_" + seq_no + extension;
						
						i = 0;
					}
				
			} else {
				try {
					sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			try {
				sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
